import json

from decorators import login_requir
from flask_restful import Resource, fields, marshal_with, request

from models import QuetionModel
from tool.LogHandler import log
from flask import jsonify


class TodoList(Resource):
    @login_requir
    def get(self):
        return {'task': 'Hello World'}

#
class GetQuestion(Resource):
    # 定义 UserModel 的 resource_fields
    user_fields = {
        'id': fields.Integer,
        'username': fields.String,
        'email': fields.String,
        'join_time': fields.DateTime
    }
    # 定义 AnswerModel 的 resource_fields
    answer_fields = {
        'id': fields.Integer,
        'content': fields.String,
        'create_time': fields.DateTime,
        'author_id': fields.Integer,
        'author': fields.Nested(user_fields)
    }
    # 定义 QuetionModel 的 resource_fields
    question_fields = {
        'id': fields.Integer,
        'title': fields.String,
        'content': fields.String,
        'create_time': fields.DateTime,
        'author_id': fields.Integer,
        'author': fields.Nested(user_fields),
        'answers': fields.List(fields.Nested(answer_fields))
    }

    # 装饰器，定义返回数据
    @marshal_with(question_fields)
    @login_requir
    def get(self):
        questions = QuetionModel.query.all()
        return questions